package com.bainuo.doctor.common.util;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {
    /**
     * 获得指定格式的日期
     *
     * @param pattern 格式模板，为null，则为默认的格式
     * @param date    毫秒数
     * @return 符合格式的日期字符串
     */
    public static String getDate(String pattern, long date) {
        pattern = pattern == null ? "yyyy-MM-dd HH:mm" : pattern;
        return new SimpleDateFormat(pattern).format(new Date(date * 1000));
    }

    public static String getDate(long date) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(date));
    }

    public static String timestampToString(Date time) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return df.format(time);
    }

    public static String getCurDateToString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        return df.format(curDate);
    }

    public static String getCurTimeToStringWithSecond() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        return df.format(curDate);
    }

    /**
     * 获取当前时间
     *
     * @return
     */
    public static String getCurTimeToStringWithMin() {
        SimpleDateFormat df = new SimpleDateFormat("MM-dd HH:mm");
        Date curDate = new Date(System.currentTimeMillis());
        return df.format(curDate);
    }

    public static String getCurTimeToString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmm");
        Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
        return df.format(curDate);
    }

    public static String getTimeWithDay(String date) {
        return date.substring(0, 10);
    }

    /**
     * @Title:converLongTimeToStr
     * @Descripton:将秒转换成时间
     * @param:@return
     * @return:String
     * @created:2014年7月18日
     */
    public static String converLongTimeToStr(int nTime) {
        int mi = 60;
        int hh = mi * 60;

        long hour = (nTime) / hh;
        long minute = (nTime - hour * hh) / mi;
        long second = nTime - hour * hh - minute * mi;

        String strHour = hour < 10 ? "0" + hour : "" + hour;
        String strMinute = minute < 10 ? "0" + minute : "" + minute;
        String strSecond = second < 10 ? "0" + second : "" + second;

        if (hour > 0) {
            return strHour + ":" + strMinute + ":" + strSecond;
        } else {
            return strMinute + ":" + strSecond;
        }
    }

    /**
     * @Title:getGapSecondCount
     * @Descripton:获取两个日期之间胡间隔秒数
     * @param:@param startDate
     * @param:@param endDate
     * @param:@return
     * @return:int
     * @created:2014年7月21日
     */
    public static int getGapSecondCount(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
        fromCalendar.set(Calendar.MINUTE, 0);
        fromCalendar.set(Calendar.SECOND, 0);
        fromCalendar.set(Calendar.MILLISECOND, 0);

        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(Calendar.HOUR_OF_DAY, 0);
        toCalendar.set(Calendar.MINUTE, 0);
        toCalendar.set(Calendar.SECOND, 0);
        toCalendar.set(Calendar.MILLISECOND, 0);

        return (int) ((toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / 1000);
    }

    public static String getComparedTimeToString(String oldDateSt) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date oldDate = df.parse(oldDateSt);
            Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
            long l = curDate.getTime() - oldDate.getTime();// 获取时间差
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            if (day > 0) {
                return oldDateSt.substring(5, oldDateSt.length());
            } else if (hour > 0) {
                return hour + "小时前";
            } else if (min > 0) {
                return min + "分钟前";
            } else {
                return "刚刚";
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            Log.i("TAG", "获取时间出错：！");
            e.printStackTrace();
        }
        return oldDateSt.substring(0, 10);

    }

    public static String getTimestampString(String oldDateSt) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
            Date oldDate = df.parse(oldDateSt);
            Date curDate = new Date(System.currentTimeMillis());// 获取当前时间
            long l = curDate.getTime() - oldDate.getTime();// 获取时间差
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
//			if (day > 0) {
//				return oldDateSt.substring(5, oldDateSt.length());
//			} else{
//				return oldDateSt.substring(11, oldDateSt.length());
//			}
            //昨天 86400000=24*60*60*1000 一天
            long create = df.parse(oldDateSt).getTime();
            Calendar now = Calendar.getInstance();
            long ms = 1000 * (now.get(Calendar.HOUR_OF_DAY) * 3600 + now.get(Calendar.MINUTE) * 60 + now.get(Calendar.SECOND));//毫秒数
            long ms_now = now.getTimeInMillis();
            if (ms_now - create < ms) {
                return oldDateSt.substring(11, oldDateSt.length());
            } else if (ms_now - create < (ms + 24 * 3600 * 1000)) {
                return "昨天 " + oldDateSt.substring(11, oldDateSt.length());
            } else {
                return oldDateSt.substring(0, oldDateSt.length());
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            Log.i("TAG", "获取时间出错：！");
            e.printStackTrace();
        }
        return oldDateSt.substring(0, oldDateSt.length());

    }

    public static String getFutrueDay(long today, long compareDay) {
        Date date = new Date(today);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int todayNo = calendar.get(Calendar.DAY_OF_MONTH);
        date = new Date(compareDay);
        calendar.setTime(date);
        int compareDayNo = calendar.get(Calendar.DAY_OF_MONTH);
        if (compareDayNo - todayNo == 1) {
            return "明天";
        } else if (compareDayNo - todayNo == 0) {
            return "今天";
        }
        String tmpDate = getDate(compareDay);
        return tmpDate.substring(0, tmpDate.length() - 6);
    }

    /**
     * 毫秒转 时分秒
     *
     * @param ms
     * @return
     */
    public static String formatTime(Long ms) {

        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        int day = (int) (ms / dd);
        int hour = (int) ((ms - day * dd) / hh);
        int minute = (int) ((ms - day * dd - hour * hh) / mi);
        int second = (int) ((ms - day * dd - hour * hh - minute * mi) / ss);
        int milliSecond = (int) (ms - day * dd - hour * hh - minute * mi - second * ss);
        String sb = new String();
        if (day > 0) {
            return day + "天";
        }
        if (hour > 0) {
            return hour + "小时";
        }
        if (minute > 0) {
            return minute + "分钟";
        }

        return "";
    }
}
